package com.ruoyi.sbxz.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sbxz.domain.ZscqSbxz;
import com.ruoyi.sbxz.domain.ZscqSbxzFj;
import com.ruoyi.sbxz.mapper.ZscqSbxzMapper;
import com.ruoyi.sbxz.service.IZscqSbxzService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 申报须知Service业务层处理
 *
 * @author huacai
 * @date 2024-10-28
 */
@Service
public class ZscqSbxzServiceImpl implements IZscqSbxzService {
    @Resource
    private ZscqSbxzMapper zscqSbxzMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询申报须知
     *
     * @param sbxzId 申报须知主键
     * @return 申报须知
     */
    @Override
    public ZscqSbxz selectZscqSbxzBySbxzId(String sbxzId) {
        return zscqSbxzMapper.selectZscqSbxzBySbxzId(sbxzId);
    }

    /**
     * 查询申报须知列表
     *
     * @param zscqSbxz 申报须知
     * @return 申报须知
     */
    @Override
    public List<ZscqSbxz> selectZscqSbxzList(ZscqSbxz zscqSbxz) {
        return zscqSbxzMapper.selectZscqSbxzList(zscqSbxz);
    }

    /**
     * 新增申报须知
     *
     * @param zscqSbxz 申报须知
     * @return 结果
     */
    @Transactional
    @Override
    public int insertZscqSbxz(ZscqSbxz zscqSbxz) {
        zscqSbxz.setCreateTime(DateUtils.getNowDate());
        zscqSbxz.setIdWhere("存在");
        int rows = zscqSbxzMapper.insertZscqSbxz(zscqSbxz);
        insertZscqSbxzFj(zscqSbxz);
        return rows;
    }

    /**
     * 批量新增申报须知
     *
     * @param zscqSbxzs 申报须知List
     * @return 结果
     */
    @Override
    public int batchInsertZscqSbxz(List<ZscqSbxz> zscqSbxzs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(zscqSbxzs)) {
            try {
                for (int i = 0; i < zscqSbxzs.size(); i++) {
                    int row = zscqSbxzMapper.insertZscqSbxz(zscqSbxzs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == zscqSbxzs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改申报须知
     *
     * @param zscqSbxz 申报须知
     * @return 结果
     */
    @Transactional
    @Override
    public int updateZscqSbxz(ZscqSbxz zscqSbxz) {
        zscqSbxz.setUpdateTime(DateUtils.getNowDate());
//        zscqSbxzMapper.deleteZscqSbxzFjBySbxzId(zscqSbxz.getSbxzId());
        insertZscqSbxzFj(zscqSbxz);
        return zscqSbxzMapper.updateZscqSbxz(zscqSbxz);
    }

    /**
     * 批量删除申报须知
     *
     * @param sbxzIds 需要删除的申报须知主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZscqSbxzBySbxzIds(String[] sbxzIds) {
        zscqSbxzMapper.deleteZscqSbxzFjBySbxzIds(sbxzIds);
        return zscqSbxzMapper.deleteZscqSbxzBySbxzIds(sbxzIds);
    }

    /**
     * 删除申报须知信息
     *
     * @param sbxzId 申报须知主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZscqSbxzBySbxzId(String sbxzId) {
        zscqSbxzMapper.deleteZscqSbxzFjBySbxzId(sbxzId);
        return zscqSbxzMapper.deleteZscqSbxzBySbxzId(sbxzId);
    }

    /**
     * 新增附件
     *
     * @param zscqSbxzFj
     * @return
     */
    @Override
    public int insertSbxzFjBySbxzId(ZscqSbxzFj zscqSbxzFj) {
        return zscqSbxzMapper.insertSbxzFjBySbxzId(zscqSbxzFj);
    }

    /**
     * 根据ID查询附件列表
     *
     * @param zscqSbxzFj
     * @return
     */
    @Override
    public List<ZscqSbxzFj> selectSbxzFjList(ZscqSbxzFj zscqSbxzFj) {
        return zscqSbxzMapper.selectSbxzFjList(zscqSbxzFj);
    }

    /**
     * 批量删除申报须知附件
     *
     * @param fjIds 需要删除的申报须知附件主键
     * @return 结果
     */
    @Override
    public int deleteZscqSbxzFjByFjIds(Integer[] fjIds) {
        return zscqSbxzMapper.deleteZscqSbxzFjByFjIds(fjIds);
    }

    /**
     * 新增申报须知附件信息
     *
     * @param zscqSbxz 申报须知对象
     */
    public void insertZscqSbxzFj(ZscqSbxz zscqSbxz) {
        List<ZscqSbxzFj> zscqSbxzFjList = zscqSbxz.getZscqSbxzFjList();
        String sbxzId = zscqSbxz.getSbxzId();
        if (StringUtils.isNotNull(zscqSbxzFjList)) {
            List<ZscqSbxzFj> list = new ArrayList<ZscqSbxzFj>();
            for (ZscqSbxzFj zscqSbxzFj : zscqSbxzFjList) {
                zscqSbxzFj.setSbxzId(sbxzId);
                list.add(zscqSbxzFj);
            }
//            if (list.size() > 0) {
//                zscqSbxzMapper.batchZscqSbxzFj(list);
//            }
        }
    }
}
